package com.example.its2021606134chh.TrafficQueryModule;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TraQueActivity extends AppCompatActivity {
    private ParkingLotShow parkingLotShow;
    private CollegeShow hospitalRoadShow;
    private CollegeShow happinessRoadShow;
    private CollegeShow lenovoRoadShow;
    private com.example.its2021606134chh.TrafficQueryModule.TQREShow TQREShow;
    private CollegeShow REShow;
    private CollegeShow collegeShow;
    private int[] roadList = {1, 2, 3, 4, 5, 6, 7};
    private Timer timer;
    private TimerTask timerTask;
    private TextView tvTQDate;
    private TextView tvTQWeek;
    private TextView tvTraTem;
    private TextView tvTraHum;
    private TextView tvTraPM25;
    private ImageView imgTQRefresh;
    private ImageView imgTQMan;
    private ImageView imgTQWoman;
    private int num = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tra_que);
        initView();
        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-M-d");
        String sNow = ft.format(dNow);
        SimpleDateFormat format = new SimpleDateFormat("EEEE");
        String week = format.format(dNow);
        tvTQDate.setText(sNow);
        tvTQWeek.setText(week);
        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
            @Override
            public void onFailure(Exception e) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(String json) {
                Gson gson = new Gson();
                ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                int Temperature = Integer.parseInt(params.getTemperature());
                int Humidity = Integer.parseInt(params.getHumidity());
                int Pm25 = Integer.parseInt(params.getPm25());
                tvTraPM25.setText(Pm25 + "μg/m3");
                tvTraTem.setText(Temperature + "℃");
                tvTraHum.setText(Humidity + "%");
            }
        }, "get_all_sense", new String[]{"UserName"}, "user1");
        imgTQRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {

                    }

                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onResponse(String json) {
                        Gson gson = new Gson();
                        ThresholdParams params = gson.fromJson(json, ThresholdParams.class);
                        int Temperature = Integer.parseInt(params.getTemperature());
                        int Humidity = Integer.parseInt(params.getHumidity());
                        int Pm25 = Integer.parseInt(params.getPm25());
                        tvTraPM25.setText(Pm25 + "μg/m3");
                        tvTraTem.setText(Temperature + "℃");
                        tvTraHum.setText(Humidity + "%");
                    }
                }, "get_all_sense", new String[]{"UserName"}, "user1");
            }
        });
        // Inflate the layout for this fragment
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (num == -1) {
                            imgTQMan.setImageResource(R.drawable.jiaojing1_1);
                            imgTQWoman.setImageResource(R.drawable.jiaojing2_1);
                            num++;
                        } else if (num == 0) {
                            imgTQMan.setImageResource(R.drawable.jiaojing1_2);
                            imgTQWoman.setImageResource(R.drawable.jiaojing2_2);
                            num = -1;
                        }
                    }
                });
                for (int road : roadList) {
                    HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                        @Override
                        public void onFailure(Exception e) {

                        }

                        @Override
                        public void onResponse(String json) {
                            Gson gson = new Gson();
                            RoadStatus status = gson.fromJson(json, RoadStatus.class);
                            int roadStatus = Integer.parseInt(status.getStatus());
                            switch (road) {
                                case 1:
                                    switch (roadStatus) {
                                        case 1:
                                            collegeShow.setState(1);
                                            break;
                                        case 2:
                                            collegeShow.setState(2);
                                            break;
                                        case 3:
                                            collegeShow.setState(3);
                                            break;
                                        case 4:
                                            collegeShow.setState(4);
                                            break;
                                        case 5:
                                            collegeShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    collegeShow.invalidate();
                                    break;
                                case 2:
                                    switch (roadStatus) {
                                        case 1:
                                            lenovoRoadShow.setState(1);
                                            break;
                                        case 2:
                                            lenovoRoadShow.setState(2);
                                            break;
                                        case 3:
                                            lenovoRoadShow.setState(3);
                                            break;
                                        case 4:
                                            lenovoRoadShow.setState(4);
                                            break;
                                        case 5:
                                            lenovoRoadShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    lenovoRoadShow.invalidate();
                                    break;
                                case 3:
                                    switch (roadStatus) {
                                        case 1:
                                            hospitalRoadShow.setState(1);
                                            break;
                                        case 2:
                                            hospitalRoadShow.setState(2);
                                            break;
                                        case 3:
                                            hospitalRoadShow.setState(3);
                                            break;
                                        case 4:
                                            hospitalRoadShow.setState(4);
                                            break;
                                        case 5:
                                            hospitalRoadShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    hospitalRoadShow.invalidate();
                                    break;
                                case 4:
                                    switch (roadStatus) {
                                        case 1:
                                            happinessRoadShow.setState(1);
                                            break;
                                        case 2:
                                            happinessRoadShow.setState(2);
                                            break;
                                        case 3:
                                            happinessRoadShow.setState(3);
                                            break;
                                        case 4:
                                            happinessRoadShow.setState(4);
                                            break;
                                        case 5:
                                            happinessRoadShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    happinessRoadShow.invalidate();
                                    break;
                                case 5:
                                    switch (roadStatus) {
                                        case 1:
                                            TQREShow.setState(1);
                                            break;
                                        case 2:
                                            TQREShow.setState(2);
                                            break;
                                        case 3:
                                            TQREShow.setState(3);
                                            break;
                                        case 4:
                                            TQREShow.setState(4);
                                            break;
                                        case 5:
                                            TQREShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    TQREShow.invalidate();
                                    break;
                                case 6:
                                    switch (roadStatus) {
                                        case 1:
                                            REShow.setState(1);
                                            break;
                                        case 2:
                                            REShow.setState(2);
                                            break;
                                        case 3:
                                            REShow.setState(3);
                                            break;
                                        case 4:
                                            REShow.setState(4);
                                            break;
                                        case 5:
                                            REShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    REShow.invalidate();
                                    break;
                                case 7:
                                    switch (roadStatus) {
                                        case 1:
                                            parkingLotShow.setState(1);
                                            break;
                                        case 2:
                                            parkingLotShow.setState(2);
                                            break;
                                        case 3:
                                            parkingLotShow.setState(3);
                                            break;
                                        case 4:
                                            parkingLotShow.setState(4);
                                            break;
                                        case 5:
                                            parkingLotShow.setState(5);
                                            break;
                                        default:
                                            break;
                                    }
                                    parkingLotShow.invalidate();
                                    break;
                                default:
                                    break;
                            }
                        }
                    }, "get_road_status", new String[]{"RoadId", "UserName"}, road, "user1");
                }
            }
        };
        timer.schedule(timerTask, 0, 3000);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            TraQueActivity.this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initView() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        parkingLotShow = (ParkingLotShow) findViewById(R.id.parkingLotShow);
        hospitalRoadShow = (CollegeShow) findViewById(R.id.hospitalRoadShow);
        happinessRoadShow = (CollegeShow) findViewById(R.id.happinessRoadShow);
        lenovoRoadShow = (CollegeShow) findViewById(R.id.lenovoRoadShow);
        TQREShow = (TQREShow) findViewById(R.id.TQREShow);
        REShow = (CollegeShow) findViewById(R.id.REShow);
        collegeShow = (CollegeShow) findViewById(R.id.collegeShow);
        tvTQDate = (TextView) findViewById(R.id.tvTQDate);
        tvTQWeek = (TextView) findViewById(R.id.tvTQWeek);
        tvTraTem = (TextView) findViewById(R.id.tvTraTem);
        tvTraHum = (TextView) findViewById(R.id.tvTraHum);
        tvTraPM25 = (TextView) findViewById(R.id.tvTraPM25);
        imgTQRefresh = (ImageView) findViewById(R.id.imgTQRefresh);
        imgTQMan = (ImageView) findViewById(R.id.imgTQMan);
        imgTQWoman = (ImageView) findViewById(R.id.imgTQWoman);
    }
}